package net.thxjj.adminServer.controller;

import com.alibaba.fastjson.JSONObject;
import net.thxjj.adminServer.util.ParameterMap;
import net.thxjj.adminServer.util.Tools;
import net.thxjj.sitermodel.Product;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import net.thxjj.adminServer.controller.base.BaseController;
import net.thxjj.adminServer.entity.ResponseModel;
import net.thxjj.adminServer.entity.ResultEnum;
import net.thxjj.adminServer.service.IProductService;

import java.util.List;

@RestController
@RequestMapping("/api/product")
public class ProductController extends BaseController {
    private String qxurl = "product/list";
	@Autowired
	private IProductService iproductService;

	/**
	 * 获取产品列表
	 * @return
	 */
	@RequestMapping(value="/getProducts",method= RequestMethod.GET)
	@ResponseBody
	public Object Products(){

		String pid = this.getParameterMap().getString("pid");

		if(StringUtils.isEmpty(pid)) return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);

		List<Product> list = iproductService.getProductList(pid);


		return ResponseModel.getModel(ResultEnum.SUCCESS, list);

	}

	/**
	 * 根据prod_key获取产品详情
	 * @return
	 */
	@RequestMapping(value="/getProductDetail",method= RequestMethod.GET)
	@ResponseBody
	public Object ProductDetail(){

		String prod_key = this.getParameterMap().getString("prod_key");

		if(StringUtils.isEmpty(prod_key)) return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);

		Product product = iproductService.getProduct(prod_key);
		if(product == null){
			return ResponseModel.getModel(ResultEnum.PRODUCT_NOT_EXIST_ERROR,null);
		}

		Long numtotal = iproductService.getNumOfDevices(prod_key);
		Long numOnline = iproductService.getNumOfOnlineDevices(prod_key);

		ParameterMap parameterMap = new ParameterMap();
		parameterMap.put("product",product);
		parameterMap.put("total",numtotal);
		parameterMap.put("onlineNum",numOnline);

		return ResponseModel.getModel(ResultEnum.SUCCESS, parameterMap);
	}


	@RequestMapping(value="/devicesTrend",method= RequestMethod.GET)
	@ResponseBody
	public Object devicesTrend() {

		String prodKey = this.getParameterMap().getString("prodKey");

		String start_time = this.getParameterMap().getString("start_time");

		String end_time = this.getParameterMap().getString("end_time");


		if (Tools.isEmpty(prodKey) || Tools.isEmpty(start_time) || Tools.isEmpty(end_time)) {
			return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR, null);
		}

		if (!Tools.isLongNumber(start_time) || !Tools.isLongNumber(end_time)) {
			return ResponseModel.getModel(ResultEnum.PARAMETER_FORMAT_ERROR, null);
		}

		Product product = iproductService.getProduct(prodKey);

		if(product == null){
			return ResponseModel.getModel(ResultEnum.PRODUCT_NOT_EXIST_ERROR,null);
		}

		long start_time_long = Long.parseLong(start_time);
		long end_time_long = Long.parseLong(end_time);

		if(end_time_long-start_time_long<3600*24*1000){
			return ResponseModel.getModel(ResultEnum.DATE_BE_IS_NOT_ALLOW_LIT_THAN_ONEDAY_ERROR,null);
		}

		if(((end_time_long-start_time_long)/(3600*24*1000))>60){
			return ResponseModel.getModel(ResultEnum.DATE_BE_IS_NOT_ALLOW_LAT_THAN_60DAY_ERROR,null);
		}




		return ResponseModel.getModel(ResultEnum.SUCCESS,iproductService.getDeviceRends(prodKey,start_time_long,end_time_long));
	}


	@RequestMapping(value="/update/setting",method= RequestMethod.POST)
	@ResponseBody
	public Object updateSetting(@RequestBody JSONObject jsonParam) {

		String prodKey = jsonParam.getString("prodKey");


		if(!jsonParam.containsKey("isForcebind")||!jsonParam.containsKey("prodKey")||!jsonParam.containsKey("heartTime")||!jsonParam.containsKey("maxAuth")||Tools.isEmpty(prodKey)){
			return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
		}

		long is_forcebind = jsonParam.getLongValue("isForcebind");
		long heart_time = jsonParam.getLongValue("heartTime");
		long max_auth = jsonParam.getLongValue("maxAuth");


		if(is_forcebind<0||is_forcebind>1||heart_time<=0||max_auth<1||max_auth>5){
			return ResponseModel.getModel(ResultEnum.PARAMETER_VALUE_ERROR,null);
		}


		Product product = iproductService.getProduct(prodKey);
		if(product==null){
			return ResponseModel.getModel(ResultEnum.PRODUCT_NOT_EXIST_ERROR,null);
		}

		ParameterMap parameterMap = new ParameterMap();
		parameterMap.put("prod_key",prodKey);
		parameterMap.put("is_forcebind",is_forcebind);
		parameterMap.put("max_auth_user",max_auth);
		parameterMap.put("heart_time",heart_time);

		return iproductService.updateSetting(parameterMap);
	}


	@RequestMapping(value="/pushRules",method= RequestMethod.GET)
	@ResponseBody
	public Object pushRules() {

		String prodKey = this.getParameterMap().getString("prod_key");

		if (StringUtils.isEmpty(prodKey)) {
			return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR, null);
		}

		Product product = iproductService.getProduct(prodKey);

		if(product == null){
			return ResponseModel.getModel(ResultEnum.PRODUCT_NOT_EXIST_ERROR,null);
		}

		return ResponseModel.getModel(ResultEnum.SUCCESS,iproductService.getAllPushRuleByProdKey(prodKey));
	}

	@RequestMapping(value="/enableRule",method= RequestMethod.POST)
	@ResponseBody
	public Object enableRule(@RequestBody JSONObject jsonParam) {

		if(!jsonParam.containsKey("pushId")||!jsonParam.containsKey("enable")){
			return ResponseModel.getModel(ResultEnum.PARAMETER_EMPTY_ERROR,null);
		}

		long id = jsonParam.getLongValue("pushId");
		long enable = jsonParam.getLongValue("enable");

		return iproductService.enablePushRule(enable,id);
	}

}
